layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //评论列表
    var tableIns = table.render({
        elem: '#replyList',
        url : 'getReplyList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'replyDate',title: '评论时间', align:'center',edit:"text"},
            {field: 'replyText', minWidth:300,title: '评论内容', align:'center',templet:"#Status"},
            {field: 'replyFromId', title: '评论名称', align:'center',edit:"text"},
            {title: '操作', minWidth:125, templet:'#replyListBar',fixed:"right",align:"center"}
        ]]
    });

    /**
     * 刷新表格
     */
    $(".refresh_btn").click(function(){
        tableIns.reload();
    })


    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            replyId = "";
        if(data.length > 0) {
            for (var i in data) {
                replyId+= ","+ data[i].replyId;
            }
            layer.confirm('确定更新选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("updataAllReply",{
                    replyId : replyId  //将需要删除的articlesId作为参数传入
                },function(result){
                    if (result.code == 100) {
                        tableIns.reload();
                        layer.close(index);
                        setTimeout(function(){
                            layer.msg("更新成功");
                        },500);
                    } else {
                        layer.msg("更新失败");
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的文章");
        }
    })


    //列表操作
    table.on('tool(replyList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'usable'){ //通过
            var check = $.ajax({
                url : "checkReply",
                type : "get",
                data : {replyId : data.replyId}
            });
            check.done(function (result) {
                if (result.code == 100){
                    tableIns.reload();
                    setTimeout(function(){
                        layer.msg("审核通过");
                    },500);

                } else {

                }
            })

        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此评论？',{icon:3, title:'提示信息'},function(index){
                $.get("deleteReplyByReplyId",{
                    replyId : data.replyId  //将需要删除的Id作为参数传入
                },function(result){
                    if (result.code == 100 ) {
                        tableIns.reload();
                        layer.close(index);
                        setTimeout(function(){
                            layer.msg("删除成功");
                        },500);
                    } else {
                        setTimeout(function(){
                            layer.msg("删除失败");
                        },500);
                    }
                })
            });
        }
    });
})
